package com.yunliuchain.examination.system.service.impl;

import java.util.*;

import com.yunliuchain.examination.common.constant.Global;
import com.yunliuchain.examination.common.res.ApiResult;
import com.yunliuchain.examination.common.utils.AESUtil;
import com.yunliuchain.examination.security.cache.LoginUserCache;
import com.yunliuchain.examination.security.req.UserLoginReq;
import com.yunliuchain.examination.system.res.UserRes;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.yunliuchain.examination.common.service.impl.BaseServiceImpl;


import com.yunliuchain.examination.system.entity.User;
import com.yunliuchain.examination.system.dao.UserDao;
import com.yunliuchain.examination.system.service.UserService;
import org.springframework.transaction.annotation.Transactional;

/**<p>Title:系统用户表 服务实现类 </p>
 * <p>Description: </p>
 * <p>作者：zzc</p>
 * <p>日期：2018-04-29 11:56:14</p>
 * <p>版本：1.0</p>
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User,Long> implements UserService {

    @Autowired
	private UserDao userDao;


    @Autowired
    private LoginUserCache userCache;


    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    @Override
    public UserRes getByLoginName(String loginName) {
        return userDao.getByLoginName(loginName);
    }





    @Override
    @Transactional(readOnly = false)
    public ApiResult save(User user) {
        String salt = generateUID().substring(0,16);
        String encrypt = AESUtil.encrypt(user.getLoginPwd(), salt);
        user.setLoginPwd(encrypt);
        user.setSalt(salt);
        return super.save(user);
    }

    @Override
    public ApiResult login(UserLoginReq loginUserReq) {
        String code = redisTemplate.opsForValue().get(Global.LOGIN_CODE_USER + loginUserReq.getPhone());
        logger.info("验证码:"+code);

        if (code==null || !code.equals(loginUserReq.getCode())){
            return ApiResult.fail("验证码有误!");
        }

        UserRes userRes = getByPhone(loginUserReq.getPhone());
        try {

            if(userRes == null){
                return ApiResult.fail("用户名或密码有误!");
            }

            String   decrypt = AESUtil.decrypt(userRes.getLoginPwd(), userRes.getSalt());
            logger.info("password:"+decrypt);
            if(StringUtils.isBlank(decrypt) || !decrypt.equals(loginUserReq.getLoginPwd())){
                return ApiResult.fail("用户名或密码有误!");
            }
            String id = generateUID();
            userCache.set(Global.LOGIN_TOKEN_USER + id, userRes);
            userRes.setLoginPwd(null);
            return ApiResult.success(userRes);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.fail("登录失败");
        }
    }

    private UserRes getByPhone(String phone) {
        return userDao.getByPhone(phone);
    }

}
